<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchMap.data" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.account"
          placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchTable()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="userInfoList" style="width: 100%">
      <el-table-column prop="account" label="登录账户"> </el-table-column>

      <el-table-column prop="userType" label="人员类型">
        <template slot-scope="scope">
          <p v-if="scope.row.userType == 1">会员账号</p>
          <p v-if="scope.row.userType == 2">普通账号</p>
          <p v-if="scope.row.userType == 6">其他账号</p>
        </template>
      </el-table-column>

      <el-table-column prop="userTime" label="最后登陆时间"></el-table-column>
      <el-table-column prop="" label="是否启用">
        <template slot-scope="scope">
            <el-switch
                @change="update(scope.row)"
                v-model="scope.row.userStatus"
                active-color="#13ce66"
                inactive-color="#ff4949">
            </el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="" label="是否会员">
        <template slot-scope="scope">
            <el-switch
                @change="update(scope.row)"
                v-model="scope.row.userType"
                active-text="会员"
                inactive-text="普通">
            </el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
            <el-popover
                placement="right"
                width="1050"
                trigger="click">
                <el-table :data="gridData">
                    <el-table-column width="100" property="userTName" label="姓名"></el-table-column>
                    <el-table-column width="110" property="userMobile" label="电话"></el-table-column>
                    <el-table-column width="200" property="userEmail" label="邮箱"></el-table-column>
                    <el-table-column width="100" property="userAge" label="年龄"></el-table-column>
                    <el-table-column width="100" property="userSex" label="性别"></el-table-column>
                    <el-table-column width="100" property="userBirthdate" label="出生日期"></el-table-column>
                    <el-table-column width="100" property="userLocation" label="用户所在地"></el-table-column>
                    <el-table-column width="100" property="userOccupation" label="职业"></el-table-column>
                    <el-table-column width="100" property="userMarriage" label="婚姻状态"></el-table-column>
                </el-table>
                <el-button  slot="reference" @click="searchUserInfo(scope.row.userId);">查看详情</el-button>
            </el-popover>
        </template>
      </el-table-column >
    </el-table>
    <!-- 分页 -->
    <el-pagination
    style="float: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.total"
    >
    </el-pagination>
  </div>
</template>
<script>
import { search, updateUser,searchUserInfo} from "@/api/user";
export default {
  data() {
    return {
      total: 0,
      searchMap: {
        page: 1,
        limit: 5,
        data: {},
      },
      userInfoList: [],
      userInfo: {}, //用户信息
      dialogFormVisible:false,
      gridData:[],
      a:false
    };
  },
  methods: {
    //搜索
    searchTable() {
        var _this = this;
        search(this.searchMap).then((res) => {
            _this.userInfoList = res.data;
            _this.total = res.total;
            _this.userInfoList.forEach(a=>{
                if(a.userStatus==1){
                    a.userStatus=true
                }else{
                    a.userStatus=false
                }

                if(a.userType==1){
                    a.userType=true
                }else{
                    a.userType=false
                }
            })
        });
        
    },
    openDetails(){
        console.log(row,event,column)
        this.a=true
    },
    //修改
    update(row){
        if(row.userStatus){
            row.userStatus=1
        }else{
            row.userStatus=2
        }
        if(row.userType){
            row.userType=1
        }else{
            row.userType=2
        }
        updateUser(row).then((res)=>{
            if (res.success) {
                this.searchTable();
                this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "success",
                });
            } else {
                this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "error",
                });
            }
        });
    },
    //查看详情
    searchUserInfo(row){
        this.gridData=[]
        var user = {"userInfoId":row}
        searchUserInfo(user).then(res=>{
            if(res.success){
                console.log(res.data)
                this.gridData.push(res.data)   
            }
        })
    },
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.searchTable();
    },
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.searchTable();
    },
  },
  created() {
    this.searchTable();
    if(!localStorage.getItem("token")){
        location.href="/#/login"
    }
  },
};
</script>